﻿//
// Copyright (c) 2024 BPergaofeng
//
#include "BPerEncoderGpu.h"
#include <bperutil/sys/BPerLog.h>
BPerEncoderGpu::BPerEncoderGpu() {

    m_encoder=NULL;
    m_sendKeyframe=0;
    bPer_trace("\nGpu encoder is created!");
}

BPerEncoderGpu::~BPerEncoderGpu() {
    bPer_delete(m_encoder);
}

void BPerEncoderGpu::sendMsgToEncoder(BPerRtcEncoderMessage *msg){
    if(msg->request==BPer_Req_Sendkeyframe){
        m_sendKeyframe=1;
    }else if(msg->request==BPer_Req_HighLostPacketRate){

    }else if(msg->request==BPer_Req_LowLostPacketRate){

    }
}

void BPerEncoderGpu::setVideoMetaData(BPerVideoMeta *pvmd) {

}

/*void BPerEncoderGpu::initX264Param(BPerVideoInfo *pvp,		BPerVideoEncInfo *penc) {

}*/
bool BPerEncoderGpu::isSupported()
{
    return false;
}
int32_t BPerEncoderGpu::init(BPerContext* pcontext,BPerVideoInfo *pvp) {
	if (m_isInit == 1)		return BPer_Ok;
    if(m_encoder==NULL) m_encoder=new BPerH264EncoderGpu(pvp,&pcontext->avinfo.enc);
    if(m_encoder) m_encoder->init();

	m_isInit = 1;

	return BPer_Ok;

}

int32_t BPerEncoderGpu::encode(BPerFrame* pframe, BPerEncoderCallback* pcallback) {
    if(m_encoder&&m_encoder->encode(pframe)>0){
        //  bPer_trace("\n%d:",pframe->nb);
         //   for(int j=0;j<50;j++) bPer_trace("%02x,",pframe->payload[j]);
        if(pcallback){
            if(pframe->nb>BPER_VIDEO_ENCODE_BUFFER_LEN)
                bPer_error("frame size is %d,ignore,drop,please increase framebuffer",pframe->nb);
            else
                pcallback->onVideoData(pframe);
        }

        return BPer_Ok;
    }
	return 1;
}
